package academic.ui.client.widgets;

import ui.client.GenericContainerTag;

import academic.ui.client.util.ClientHelper;
import ecommerce.soco.shared.GWT_Product;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;

public class YourPurchase extends Composite {

	private GenericContainerTag div = new GenericContainerTag("div");
	public YourPurchase() {
		div.setStyleName("purchaseBox");
		initWidget(div);
	}
	
	TextBox quantity = new TextBox();
	Double price;
	GenericContainerTag total;
	FlexTable purchaseTable = new FlexTable();
	
	public void setup(GWT_Product product) {
		price=product.getPrice0();
		div.clear();
		GenericContainerTag h2 = new GenericContainerTag("h2","Your Purchase");
		purchaseTable.clear();
		purchaseTable.setWidth("100%");
		purchaseTable.setWidget(0, 1, new GenericContainerTag("span","The Deal"));
		purchaseTable.setWidget(0, 2, new GenericContainerTag("span","Price"));
		purchaseTable.setWidget(0, 3, new GenericContainerTag("span","Quantity"));
		purchaseTable.setWidget(0, 4, new GenericContainerTag("span","Total"));

		Image img = new Image(GWT.getModuleBaseURL()+"../PICS/Products/"+product.getImageSmall());
		img.setSize("100px", String.valueOf(product.getSmallImgRatio()*100)+"px");
	
		quantity.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(event.getNativeEvent().getKeyCode()<49 || event.getNativeEvent().getKeyCode()>57)
				{
					event.preventDefault();
					event.stopPropagation();
				}
				else
					total.getElement().setInnerHTML(getTotal(String.valueOf(event.getCharCode())));
			}
			
		});
		
		quantity.setValue("1");
		quantity.setWidth("20px");
		quantity.setMaxLength(1);
		total = new GenericContainerTag("span",getTotal("1"));
		purchaseTable.setWidget(1, 0, img);
		purchaseTable.setWidget(1, 1, new GenericContainerTag("span",product.getName()));
		purchaseTable.setWidget(1, 2, new GenericContainerTag("span",ClientHelper.formatPrice(product.getPrice0())));
		purchaseTable.setWidget(1, 3, quantity);
		purchaseTable.setWidget(1, 4, total);
		if(!hideHeader)
			div.add(h2);
		div.add(purchaseTable);
	}
	
	public void setup(GWT_Product product, String quantityInt) {
		setup(product);
		quantity.setStyleName("viewOnlyTextBox");
		purchaseTable.setStyleName("receiptTable");
		quantity.setValue(quantityInt);
		quantity.setEnabled(false);
		total.getElement().setInnerHTML(getTotal(quantityInt));
	}
	private String getTotal(String quantity) {
		return ClientHelper.formatPrice(Integer.parseInt(quantity)*price);
	}
	
	public Integer getQuantity() {
		return Integer.parseInt(quantity.getText());
	}
	
	Boolean hideHeader = false;

	public void hideHeader() {
		hideHeader=true;
	}
	
	
}
